package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.Formance;
import cn.stylefeng.guns.sys.modular.system.mapper.FormanceMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.FormanceParam;
import cn.stylefeng.guns.sys.modular.system.model.result.FormanceResult;
import  cn.stylefeng.guns.sys.modular.system.service.FormanceService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 绩效 服务实现类
 * </p>
 *
 * @author liuy
 * @since 2021-04-16
 */
@Service
public class FormanceServiceImpl extends ServiceImpl<FormanceMapper, Formance> implements FormanceService {
    private String userName="admin";
    @Override
    public void add(FormanceParam param){
        Formance entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FormanceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FormanceParam param){
        Formance oldEntity = getOldEntity(param);
        Formance newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FormanceResult findBySpec(FormanceParam param){
        return null;
    }

    @Override
    public List<FormanceResult> findListBySpec(FormanceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FormanceParam param){
        Page pageContext = getPageContext();
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        String roleName="人事";
        IPage page;
        param.setMonth(lastMonth);
        if(roleNames.contains(roleName)){
           page=this.baseMapper.customPageList(pageContext, param);
            return LayuiPageFactory.createPageInfo(page);
        }
        if(!(userName.equals(account))){
            param.setFlag("8");
            //根据姓名查询是否是督导
            if(!roleNames.contains("督导")){
                param.setUsername(username);
                page=this.baseMapper.customPageList(pageContext, param);
                if(page.getRecords().size()==0){
                    param.setUsername(account);
                    page=this.baseMapper.customPageList(pageContext, param);
                }
                return LayuiPageFactory.createPageInfo(page);
            }else{
                param.setDdName(username);
                page=this.baseMapper.customPageList(pageContext, param);
                if(page.getRecords().size()==0){
                    param.setTel(account);
                    page=this.baseMapper.customPageList(pageContext, param);
                }
                return LayuiPageFactory.createPageInfo(page);
            }
        }else{
            page=this.baseMapper.customPageList(pageContext, param);
            return LayuiPageFactory.createPageInfo(page);
        }
    }

    @Override
    public List<Map<String, Object>> customMapList(FormanceParam param) {
        List<Map<String, Object>>list;
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        String roleName="人事";
        param.setMonth(lastMonth);
        if(roleNames.contains(roleName)){
            list=this.baseMapper.customMapList(param);
            return list;
        }
        if(!(userName.equals(account))){
            param.setFlag("8");
            //根据姓名查询是否是督导
            if(!roleNames.contains("督导")){
                param.setUsername(username);
                list=this.baseMapper.customMapList(param);
                if(list.size()==0){
                    param.setUsername(account);
                    list=this.baseMapper.customMapList(param);
                }
                return list;
            }else{
                param.setDdName(username);
                list=this.baseMapper.customMapList(param);
                if(list.size()==0){
                    param.setTel(account);
                    list=this.baseMapper.customMapList(param);
                }
                return list;
            }
        }else{
            list=this.baseMapper.customMapList(param);
            return list;
        }
    }

    private Serializable getKey(FormanceParam param){
        return null;
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Formance getOldEntity(FormanceParam param) {
        return this.getById(getKey(param));
    }

    private Formance getEntity(FormanceParam param) {
        Formance entity = new Formance();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
